/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package ocr_modelcreator;

import com.Zhang.Suen.thinning.ThinningService;
import com.azon.guesser.utils.FilledChecker;
import com.azon.guesser.utils.ThinnedChecker;
import com.chain.code.utils.FilledWetLeftHand;
import java.io.IOException;
import com.azon.ip.utils.ImageReader;
import com.azon.ip.utils.ImageWriter;
import com.chain.code.utils.MatrixConverter;
import com.azon.ip.utils.Printer;
import com.chain.code.utils.ThinnedWetLeftHand;
import com.chain.code.utils.WetLeftHand;
import java.awt.image.BufferedImage;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Mahdan Ahmad F A
 */
public class OCR_modelCreator {

    /**
     * @param args the command line arguments
     * @throws java.io.IOException
     */
    public static void main(String[] args) throws IOException {
        
        /*
        WetLeftHand wet = new ThinnedWetLeftHand();
        
        String besar = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String kecil = "abcdefghijklmnopqrstuvwxyz";
        String angka = "0123456789";
        
        int[][] matrix_grayscale = null;
        int[][] matrix_converted = null;
        
        HashMap<String, String> map0 = new HashMap<>();
        HashMap<String, String> map1 = new HashMap<>();
        HashMap<String, String> map2 = new HashMap<>();
        HashMap<String, String> map3 = new HashMap<>();
        
        try (PrintWriter writer = new PrintWriter("the-file-name.csv", "UTF-8")) {
            writer.println("Titik, Garis Horizontal, Garis Vertical, Garis Miring, Cembung Kanan, Cembung Kiri, Cembung Atas, Cembung Bawah, Karakter (nilai), Karakter, Index");
            
            for (int i = 0; i < besar.length(); i++) {
                Printer.printToCSV("result\\" + besar.charAt(i) + " besar.jpg", writer, wet, matrix_grayscale, matrix_converted, true, map0, map1, map2, map3);
            }
            
            for (int i = 0; i < kecil.length(); i++) {
                Printer.printToCSV("result\\" + kecil.charAt(i) + " kecil.jpg", writer, wet, matrix_grayscale, matrix_converted, true, map0, map1, map2, map3);
            } 
            
            for (int i = 0; i < angka.length(); i++) {
                Printer.printToCSV("result\\" + angka.charAt(i) + ".jpg", writer, wet, matrix_grayscale, matrix_converted, true, map0, map1, map2, map3);
            }
        } // */
        
        /*
        String coba     = "2222222222222222222222222447487884887884888488488488848848484142212242212242212212121414488888888888888888888888-";
        String sequence = ThinnedChecker.getSequence(coba);
        
        System.out.println(coba);
        System.out.println(sequence);
        System.out.println(ThinnedChecker.getEnhancedSequence(sequence));
        // */
        
        
        String path = "result\\A besar.jpg";
        //String path = "coba.jpg";
        
        WetLeftHand wet             = new ThinnedWetLeftHand();
        
        int[][] matrix_grayscale    = ImageReader.ReadImageAsGrayScale(path);
        int[][] matrix_converted    = MatrixConverter.blackAndWhiteConverter(matrix_grayscale);
              
        String[] result = wet.doWetLeftHand(matrix_converted);
        
//        for (int i = 0; i < result.length; i++) {
//            System.out.println(result[i]);
//        } 
        
        HashMap<String, String> map0 = ThinnedChecker.doCheck(result[0]);
        HashMap<String, String> map1 = ThinnedChecker.doCheck(result[1]);
        HashMap<String, String> map2 = ThinnedChecker.doCheck(result[2]);
        HashMap<String, String> map3 = ThinnedChecker.doCheck(result[3]); 
        
        Printer.printMap(map0);
        Printer.printMap(map1);
        Printer.printMap(map2);
        Printer.printMap(map3); // */
        
        //printMatrix(matrix_converted);
        

        
        //String sequence = "GH412BEFHGDC696FEBACD3";
        //String sequence = "69696984141474787898989696363638747414141212121232323236363";
        //String sequence = "6969632898121214147474787878789898969696363287474741414121212123232323636363";
        //String sequence = "6969878796989874742";
        //System.out.println("Sequence : " + sequence);
        //System.out.println("Bawah  : " + FilledChecker.cekKurvaCembungBawah(sequence));
        //System.out.println("Atas   : " + FilledChecker.cekKurvaCembungAtas(sequence));
        //System.out.println(FilledChecker.getEnhancedSequence(sequence));
        
        /*
        String pattern = "ABCDEFGH";
        
        String first_pattern    = pattern.substring(0,3);
        String second_pattern   = pattern.substring(1,4);
        String third_pattern    = pattern.substring(4,7);
        String fourth_pattern   = pattern.substring(5,8);
        
        System.out.println(first_pattern);
        System.out.println(second_pattern);
        System.out.println(third_pattern);
        System.out.println(fourth_pattern); // */
    }
    
    private static void createThinningCharacter(String filename) {
        try {
            String filepath = "image//" + filename + ".jpg";
            
            int[][] matrix          = ImageReader.ReadImageAsGrayScale(filepath);
            
            ThinningService thinner     = new ThinningService();
            
            int[][] matrix_blackandwhite    = MatrixConverter.blackAndWhiteConverter(matrix);
            int[][] thinned_matrix          = thinner.doZhangSuenThinning(MatrixConverter.blackAndWhiteReverser(matrix_blackandwhite));
            int[][] reversed_matrix         = MatrixConverter.blackAndWhiteReverser(thinned_matrix);
            int[][] new_matrix              = MatrixConverter.blackAndWhiteReverter(reversed_matrix);
            
            BufferedImage new_img = ImageWriter.writeImageGrayScale(new_matrix);
            ImageWriter.WriteImage(new_img, "result//" + filename + ".jpg");
        } catch (IOException ex) {
            Logger.getLogger(OCR_modelCreator.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
}
